<?php
/*==========================================================================*\
| AGRIVIET - CONTENT MANAGERMENT SYSTEM						
\*==========================================================================*/
$input = "3-10-2008";
$date =date("Y",strtotime($input));
echo $date; 
//echo"=";
//echo date("dmY",strtotime($input));

//echo strtotime($input);

//echo time(); echo "=";
//echo mktime();

	$pathdb = "uploads/news"; // Duong dan ghi vao database		
	$imagepath="../uploads/news/"; // Duong dan de upload Image
	$upload_allow_extension = "jpg,png,gif,pdf"; // Cac phan mo rong duoc cho phep
	
	$id=$_GET['id'];

if (isset($_POST['butSave'])) {
	$published = $_POST['published'];
	$hit = $_POST['hit'];
	$date_add = $_POST['date_add'];
	
	$ngay = $_POST['ngay'];
	$thang = $_POST['thang'];
	$nam = $_POST['nam'];
	$input= "{$ngay}-{$thang}-{$nam}";
	$date_add=strtotime($input);
	
	$catid = $_POST['catid'];
	$title = trim($_POST['title']);
	$vbb_id = $_POST['vbb_id'];
	
	$khu = $_POST['khu'];
	
	$numcv2 = $_POST['numcv2'];
	$ordering = $_POST['ordering'];
	

	
	$cat = getCat($catid,'cv_bql'); 
	//$catP = getC($cat->id);
	
	$catp = mysql_fetch_object(mysql_query("SELECT * FROM cv WHERE id=".$cat->parent_id));
	//$catp->alias;
	if($catp->alias!='')
	$catlink = $catp->alias."/".$cat->alias;
	else
	$catlink = $cat->alias;
	
	if($_POST['alias']!='')
	$alias = trim($_POST['alias']);
	else
	$alias = vietdecode(trim($_POST['title']));
	
	$intro = trim($_POST['intro']);
	$body = trim(($_POST['body']));
	
	$err="";	
	if ($title=="") $err .="Xin nhập tiêu đề bằng<br>";	
	if ($intro =="") $err .="Xin nhập intro bằng<br>";	
	

	
	if ($err==''){
		/* Cap nhat hoac them moi */
		if (!empty($_POST['id'])) {
			$oldid = $_POST['id'];
			$sql = "update cv set  published='".$published."',hit='".$hit."',title='".$title."',alias='".$alias."',intro='".$intro."',";
			$sql.= "catlink='".$catlink."', body='".$body."', date_add='".$date_add."',catid='".$catid."', numcv1='".$numcv1."', numcv2='".$numcv2."'	 where id='".$oldid."'";
			 
			 echo "XXXXXXXXXX";
		}else {
		
		
		
			// Insert data
			$sql = "insert into cv (title,alias,intro,catid,khu)";
			$sql.= "values ('".$title."','".$alias."','".$intro."','".$catid."','".$khu."')";
			
			//echo $sql;
			//exit;
		}
		/* EOF - Cap nhat hoac them moi */
		if(mysql_query($sql)){ // Neu thuc hien query thanh cong
		
			if(empty($_POST['id'])) $oldid = mysql_insert_id();
			
			/*if(!empty($_POST['id'])){
			// upldate
				foreach ($_POST['title'] as $langid => $title){		
						$intro = trim($_POST['intro'][$langid]);
						$body = trim($_POST['body'][$langid]);		
						$sql = "update news_des set title='".$title."',intro='".$intro."',body='".$body."'";
						$sql .= "where id='".$oldid."' and  languages_id='".$langid."'";
						mysql_query($sql);
					}	
			} else {
			// Insert			
				foreach ($_POST['title'] as $langid => $title){						
					$sql = "insert into news_des (id,languages_id,title,intro,body)";
					$sql .= "values('".$oldid."','".$langid."','".$title."','".$intro."','".$body."')";
					mysql_query($sql);
				}	
			}*/
			
		//Upload image here
		$filename="";
		if(is_uploaded_file($_FILES['img']['tmp_name']))
		{
			//Check file extension
			if (check_upload_extension($_FILES['img']['name'],$upload_allow_extension) == 1){
			
			$filename = generate_name('news',$oldid,$_FILES['img']['name']);
			move_uploaded_file($_FILES['img']['tmp_name'],$imagepath.'' . $filename);
			//resize_image($imagepath.'large/'.$filename,$imagepath.'small/'.$filename,50,40,100);
			//resize_image($imagepath.'large/'.$filename,$imagepath.'large/'.$filename,120,120,100);

			$_POST["img"] = $filename;
			
			//echo "<HR>@@@@@@@@		$imagepath	{$_POST["img"]} ";
			//exit;
					
			$sqlUpdateField = " image ='".$filename."'";
			
				if($sqlUpdateField!='')
				{
					$sqlUpdate = "update news set $sqlUpdateField where id='".$oldid."'";
					$db->query($sqlUpdate);
				}
			}
		} else {
			$_POST["img"] = "";
		}
		// Eof Upload image
		} // Neu thuc hien Query thanh cong
		else {
			$err =  "Không th&#7875; c&#7853;p nh&#7853;t <BR>{$sql}";
		}
		
	} // if ($err=='')
	
	if ($err==''){
	$res = $db->query("SELECT * FROM cv_bql WHERE parent_id=50");
	while($r=$db->fetchNextObject($res)){
	$strCat .='catid='.$r->id.' or ';
	}
	//$strCat=$strCat.' catid=50';
	//$strSQL = "SELECT * from news WHERE $strCat order by date_add DESC limit 7";
	///echo homelist($strSQL,'../modules/html/news.htm');
	
	$strCat='';
	
	$res = $db->query("SELECT * FROM cv_bql WHERE parent_id=36");
	while($r=$db->fetchNextObject($res)){
	$strCat .='catid='.$r->id.' or ';
	}
	$strCat=$strCat.' catid=36';
	$strSQL = "SELECT * from cv WHERE $strCat order by date_add DESC limit 7";
	//echo homelist($strSQL,'../modules/html/tutorial.htm');
	
	 echo '<script>window.location="index.php?act=cv&catid='.$_REQUEST['catid'].'&code=1"</script>';
	 } 	else {
	  echo "<p align=center class='red'>".$err."</p>";
	  }

} 
		
 ?>
<!-- TinyMCE -->
<script language="javascript" type="text/javascript" src="tinymce/tiny_mce.js"></script>
<script language="javascript" type="text/javascript">
	tinyMCE.init({
		theme : "advanced",
		mode : "exact",
		elements : "body",
		document_base_url : "<?=$url;?>/",
		relative_urls : false,
		remove_script_host : false,
		save_callback : "TinyMCE_Save",
		invalid_elements : "script,applet,iframe",
		theme_advanced_toolbar_location : "top",
		theme_advanced_source_editor_height : "550",
		theme_advanced_source_editor_width : "750",
		directionality: "ltr",
		force_br_newlines : "false",
		force_p_newlines : "true",
		content_css : "template_css.css", 
		debug : false,
		cleanup : true,
		cleanup_on_startup : false,
		safari_warning : false,
		plugins : "imanager, style,layer,table,save,advhr,advlink,emotions,iespell,insertdatetime,preview,flash,searchreplace,print,paste,directionality,fullscreen,noneditable",
		theme_advanced_buttons1_add_before : "save,newdocument,separator",
		theme_advanced_buttons1_add : "fontselect,fontsizeselect",
		theme_advanced_buttons2_add : "separator,insertdate,inserttime,preview,separator,forecolor,backcolor",
		theme_advanced_buttons2_add_before: "cut,copy,paste,pastetext,pasteword,separator,search,replace,separator",
		theme_advanced_buttons3_add_before : "tablecontrols,separator",
		theme_advanced_buttons3_add : "imanager,emotions,iespell,flash,advhr,separator,print,separator,ltr,rtl,separator,fullscreen",
		theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,styleprops",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_path_location : "bottom",
		content_css : "../templates/dotMine/css/default.css",
	    plugin_insertdate_dateFormat : "%Y-%m-%d",
	    plugin_insertdate_timeFormat : "%H:%M:%S",
		extended_valid_elements : "hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
		external_link_list_url : "example_link_list.js",
		external_image_list_url : "example_image_list.js",
		flash_external_list_url : "example_flash_list.js",
		file_browser_callback : "fileBrowserCallBack",
		theme_advanced_resize_horizontal : false,
		theme_advanced_resizing : true
	});

	/*function fileBrowserCallBack(field_name, url, type, win) {
		// This is where you insert your custom filebrowser logic
		alert("Example of filebrowser callback: field_name: " + field_name + ", url: " + url + ", type: " + type);

		// Insert new URL, this would normaly be done in a popup
		win.document.forms[0].elements[field_name].value = "someurl.htm";
	}*/
	function TinyMCE_Save(editor_id, content, node)
	{
		base_url = tinyMCE.settings['document_base_url'];
		var vHTML = content;
		if (true == true){
			vHTML = tinyMCE.regexpReplace(vHTML, 'href\s*=\s*"?'+base_url+'', 'href="', 'gi');
			//vHTML = tinyMCE.regexpReplace(vHTML, 'src\s*=\s*"?'+base_url+'', 'src="', 'gi');
			vHTML = tinyMCE.regexpReplace(vHTML, 'mce_real_src\s*=\s*"?', '', 'gi');
			vHTML = tinyMCE.regexpReplace(vHTML, 'mce_real_href\s*=\s*"?', '', 'gi');
		}
		return vHTML;
	}
</script>
<!-- /TinyMCE -->
<? action('CÔNG VĂN'); // Tieu de trang


	if (isset($_GET['id'])){
	$result=mysql_query("SELECT * FROM cv WHERE id=$id");
	$row=mysql_fetch_object($result);
	$image=$row->image;
	$imagelarge=$row->imagelarge;
	$title=$row->title;
	$alias=$row->alias;
	$intro=$row->intro;
	$body=$row->body;
	$published=$row->published;
	$hit=$row->hit;
	$catid=$row->catid;
		$numcv1=$row->numcv1;
			$numcv2=$row->numcv2;
			$khu=$row->khu;
			
	//####### XY LY NGAY THANG
		$date_add=$row->date_add;
		
		
		$nam=date("Y",$date_add);
		$thang=date("m",$date_add);
				$ngay=date("d",$date_add);
		
		//##########
	
	$parent=$catid;
	$vbb_id=$row->vbb_id;



	
	}
	else 
	{
	 if ($nam==0) $nam=date(Y);
	 if ($thang==0) $thang=date(m);
		 
		 }
	 $parent=$catid;
	 

	 
	
	 

	?>
	
	
	
	
<style type="text/css">
<!--
.style1 {
	color: #FF0000;
	font-weight: bold;
}
-->
</style>
	
  <table border="0" cellpadding="3" cellspacing="3" bordercolor="#CCCCCC">
<form action="" method="post" enctype="multipart/form-data" name="form1">
<input type="hidden" name="act" value="cv_m">
<input type="hidden" name="id" value="<? echo $_REQUEST['id']; ?>">
<input type="hidden" name="page" value="<? echo $_REQUEST['page']; ?>">
    
<tr>
      <td width="119" align="right" valign="top"><div align="left">Tên Công Viên </div></td>
      <td width="499">
	  <div style="margin:3px 0">
	    <input name="title" type="text" style="width:95%" value="<?=$title;?>" size="50">
	  </div>	   </td>
    </tr>
	<tr>
	  <td align="right"><div align="left">Đơn vị quản lý </div></td>
	  <td ><select size="1" name="catid">
		<?
		echo "<option value='0'>[Chọn đơn vị quản lý công viên]</option>";

			$cats=getListCat('','cv_bql');
		foreach ($cats as $cat)
		{
			if ($cat[0]==$parent)
				echo "<option value=".$cat[0]." selected>".$cat[1]."</option>";
			else
				echo "<option value=".$cat[0].">".$cat[1]."</option>";
		}
		?>		
		</select></td>
    </tr>
	
	
	
	<tr>
	  <td align="right"><div align="left">Địa bàn </div></td>
	  <td ><select size="1" name="khu">
	  <?
		echo "	  <option value='0'>[Địa bàn]</option>";
?>
		
		
		
	
	  <option value='1' <? if($khu==1) echo "selected"; ?> >Khu QLGTĐT 1</option>
	  <option value='2' <? if($khu==2) echo "selected"; ?>>Khu QLGTĐT 2</option>
	  <option value='3' <? if($khu==3) echo "selected"; ?>>Khu QLGTĐT 3</option>
	  <option value='4' <? if($khu==4) echo "selected"; ?>>Khu QLGTĐT 4</option>
	  </select>		</td>
    </tr>
		
  
    <tr>
      <td align="right" valign="top"><div align="left">Ghi chú </div></td>
      <td valign="top" >
	  <textarea name="intro" rows="5" id="intro" style="width:95%"><?=$intro;?></textarea>	</td>
    </tr>
	<!--
    <tr>
      <td align="right" valign="top"><div align="left">Body text </div></td>
      <td >

	  <textarea name="body" id="body" style="width:95%" ><?=$body;?></textarea><br />	  </td>
    </tr>-->
	 
	 <!--
	<tr>
      <td align="right"><div align="left">Đính kèm </div></td>
      <td >
	  <? if($image!='') {?>
	  <a href="../uploads/news/<?=$image;?>" target="_blank">
	  <?
	  if (strstr($image,".pdf"))
	  echo  "<img src=\"../images/pdf.jpg\" border=\"0\" />";
	  else echo  "<img src=\"../images/att.png\" border=\"0\" />";
	  ?>
	 Click here</a><BR /><br /><? } ?>
	 
	  <input name="img" type="file" id="img"></td>
    </tr>
	
	<tr>
	  <td align="right"><div align="left">Xuất bản</div></td>	  
	  <td ><input name="published" type="checkbox" id="published" value="1" checked="<? if ($published>0) echo 'checked' ?>" /></td>
    </tr>
	
	<!---
	<tr>
	  <td align="right"><div align="left">Hit</div></td>
	  <td ><input name="hit" type="text" id="hit" value="<?=$hit;?>" /></td>
    </tr>-->
	 <tr>
      <td><div align="left">Ordering</div></td>
     
      <td><input name="ordering" type="text" size="5" value="<?=$ordering;?>" /></td>
    </tr>
    <tr>
      <td><div align="left"></div></td>
      <td><label>
        <input name="butSave" type="submit" id="butSave" value="   Cập nhật  " class="button">
        <input name="date_add" type="hidden" id="date_add" value="<?=mktime();?>" />
      </label></td>
    </tr></form>
</table>

